<?php
namespace app\admin\controller;
use think\Db;

class Reward extends Base{

    public function index(){
        $map = [];
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = Db::name('reward')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $lists = Db::name('reward')->field('id,title,rate,description,val,concat("'.getConfig('IMG_HEAD').'",img) img')->where($map)->page($Nowpage,$limits)->order('orderby desc')->select();;
        foreach($lists as $k => $v){
            $lists[$k]['val'] = $v['val']/100;
        }
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        if(input('get.page'))
        {
            return json($lists);
        }
        return $this->fetch();
    }


    /**
     *
     * @return mixed|\think\response\Json
     */
    public function add()
    {
        if(request()->isAjax()){

            $param = input('post.');
            Db::name('reward')->insert($param);
            return json(['code' => 1, 'data' => '', 'msg' => '']);
        }
        return $this->fetch();

    }


    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function edit()
    {
        if(request()->isPost()){
            $param = input('post.');
            $flag = Db::name('reward')->update($param);
            return json(['code' => 1, 'data' => '', 'msg' => '']);
        }
        $id = input('param.id');
        $reward = Db::name('reward')->field('id,title,rate,description,val,concat("'.getConfig('IMG_HEAD').'",img) img')->where('id','eq',$id)->find();
        $this->assign('reward',$reward);
        return $this->fetch();
    }


    public function delete(){
        $id = input('post.id');
        Db::name('reward')->where('id','eq',$id)->delete();
        return json(['code'=>1,'msg'=>'删除成功','data'=>'']);
    }
}